//
//  DWKVOViewController.m
//  OCAPPDemo
//
//  Created by 杨冬武 on 2021/5/17.
//

#import "DWKVOViewController.h"
#import "DWKVOPerson.h"
#import "NSArray+DWKeyValueException.h"

@interface DWKVOViewController ()

@property (nonatomic, strong) DWKVOPerson *person;
@property (weak, nonatomic) IBOutlet UIButton *racButton;
@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (nonatomic, strong) NSMutableArray <DWKVOPerson *>*mArr;

@end

static NSString *keyPath_ = @"fullName";


@implementation DWKVOViewController

- (void)dealloc {
    NSLog(@"%s", __FUNCTION__);

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%@", self.p);
    self.mArr = [NSMutableArray array];
    for (int i = 0; i < 4; i++) {
        DWKVOPerson *p = [[DWKVOPerson alloc] init];
        [self.mArr addObject:p];
    }
    self.person = [DWKVOPerson new];
//    self.person.firstName = @"Jack";
//    self.person.lastName = @"Yang";
   
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    NSLog(@"%@-%@", keyPath, change);
}

#pragma mark - 添加和移除

- (IBAction)addObserver:(id)sender {
    NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, 2)];
    [self.mArr addObserver:self toObjectsAtIndexes:indexSet forKeyPath:keyPath_ options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial context:nil];
    
    
//    [self.person addObserver:self forKeyPath:@"fullName" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
//    [self.person addObserver:self forKeyPath:@"fullName" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    
//    [self.person addObserver:self forKeyPath:@"firstName" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
//
//    [self.person addObserver:self forKeyPath:@"lastName" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

}
- (IBAction)removeObserver:(id)sender {
    NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, 2)];
    [self.mArr removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:keyPath_];
    
    //[self.person removeObserver:self forKeyPath:@"fullName"];
    
//    [self.person removeObserver:self forKeyPath:@"firstName"];
//    [self.person removeObserver:self forKeyPath:@"lastName"];
}

#pragma mark - 改变操作

- (IBAction)changeFullName:(id)sender {
    self.person.fullName = @"Dov·Yang";
}
- (IBAction)changeFirstName:(id)sender {
    self.person.firstName = @"Dov";
}
- (IBAction)changeLastName:(id)sender {
    self.person.lastName = @"Young";
}
- (IBAction)clickSetAge:(id)sender {
    [self.person setValue:@1 forKey:@"age"];
    //NSLog(@"%d", [[_person valueForKey:@"age"] intValue]);
}
- (IBAction)clickGetAge:(id)sender {
    NSLog(@"%d", [[_person valueForKey:@"age"] intValue]);
}
- (IBAction)printPerson:(id)sender {
//    [self.person printPersion];
    
    self.mArr.firstObject.fullName = @"a";
    
}


#pragma mark - KVC

- (IBAction)clickSet:(id)sender {
    
    NSMutableDictionary *mDic = [NSMutableDictionary new];
    [mDic setObject:@"b" forKey:@9];
    NSLog(@"%@", mDic);
    [mDic setValue:@0 forKey:@"a"];
    NSLog(@"%@", mDic);
    //[self setValue:@"a" forKey:@"a"];
    
}

- (IBAction)clickGet:(id)sender {
    
//    NSString *str = @"jack";
//    NSString *ustr = [str valueForKey:@"capitalizedString"];
//    ustr = [str valueForKey:@"uppercaseString"];
//    NSLog(@"%@", ustr);
//
//    NSArray *arr = @[@"jack", @"john", @"rose"];
//    NSArray *narr = [arr valueForKey:@"capitalizedString1"];
//    NSLog(@"old %@, new %@", arr, narr);
    
    
}

- (IBAction)clickPrint:(id)sender {
}

//- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
//    NSLog(@"%s", __FUNCTION__);
//}
//
//- (id)valueForUndefinedKey:(NSString *)key {
//    NSLog(@"%s", __FUNCTION__);
//    return nil;
//}

@end
